package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.FaultType;
import com.mxpio.erp.common.equipment.service.FaultTypeService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;


@Tag(name = "FaultTypeController}", description = "故障类型信息")
@RestController
@RequestMapping("/erp/equipment/faulttype/")
public class FaultTypeController {

    @Autowired
    private FaultTypeService faultTypeService;


    @GetMapping("list")
    @Operation(summary = "故障类型信息列表", description = "获取故障类型信息列表（无分页）", method = "GET")
    public Result<List<FaultType>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<FaultType> list = faultTypeService.list(FaultType.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "故障类型信息列表", description = "获取故障类型信息列表（分页）", method = "GET")
    public Result<Page<FaultType>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<FaultType> pageResult = faultTypeService.listPage(FaultType.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据编码获取故障类型信息列表", description = "根据编码获取故障类型信息列表", method = "GET")
    public Result<FaultType> getById(@PathVariable(name = "id", required = true) String id) {
        FaultType faultType = faultTypeService.getById(FaultType.class, id);
        return Result.OK(faultType);
    }

    @RequestMapping("add")
    @Operation(summary = "保存故障类型信息", description = "保存故障类型信息")
    public Result<FaultType> add(@RequestBody FaultType faultType) {
            if (StringUtils.isBlank(faultType.getTypeCode())){
                return Result.error("故障编码不能为空！");
            }
            faultTypeService.save(faultType,new SmartCrudPolicyAdapter(){
        });
        return Result.OK(faultType);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑故障类型信息", description = "编辑故障类型信息", method = "PUT")
    public Result<FaultType> edit(@RequestBody FaultType faultType) {
        faultTypeService.update(faultType);
        return Result.OK(faultType);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据编码删除故障类型信息", description = "删除故障类型信息,多个编码用英文逗号分隔", method = "DELETE")
    public Result<?> remove(@PathVariable(name = "ids", required = true) String ids) {
        return faultTypeService.deleteByIds(ids);
    }


}
